package com.sys.servlet.mgr.device;

import com.sys.pojo.JsonResult;
import com.sys.service.CustomerService;
import com.sys.service.DeviceService;
import com.sys.utils.WriteJson;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

@WebServlet(urlPatterns = "/mgr/device/modifyById")
@MultipartConfig
public class ModifyByIdServlet extends HttpServlet {
    public static final String UPLOAD = "/upload";
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Part head = req.getPart("head");
        // 此路径是头像路径，保存到数据库中，如：/upload/a0ad7e629a624b49b71d850d2feee9df.png
        String fileDB = null;
        if(head!=null) {
            // 上传文件才执行下面代码
            if(head.getSubmittedFileName()!=null && !"".equals(head.getSubmittedFileName())) {
                // 在当前工程下创建upload文件夹
                // upload文件夹的绝对路径（全路径）
                String upload = req.getServletContext().getRealPath(UPLOAD);
                File uploadFile = new File(upload);
                if (!uploadFile.exists()) {
                    // 创建文件夹
                    uploadFile.mkdirs();
                }

                String uuid = UUID.randomUUID().toString().replace("-", "");
                //截取文件后缀
                // 从右往左找 最后一个  .
                int pos = head.getSubmittedFileName().lastIndexOf(".");
                // 截取后缀
                String suffix = head.getSubmittedFileName().substring(pos);
                // 拼装新文件名，uuid+原文件名的后缀
                String newFileName = uuid+suffix;

                fileDB = UPLOAD+"/"+newFileName;
                // 拼装的是保存上传文件的全路径
                File file = new File(uploadFile,newFileName );
                // 保存
                head.write(file.getAbsolutePath());

                //文件名称
                System.out.println(head.getSubmittedFileName());
                //文件大小
                System.out.println(head.getSize());
            }
        }
        PrintWriter writer = resp.getWriter();

        String dname = req.getParameter("dname");
        String setup_time = req.getParameter("setup_time");
        String lastrepeat_time = req.getParameter("lastrepeat_time");
        String fix_time = req.getParameter("fix_time");
        String istrouble = req.getParameter("istrouble");
        String did = req.getParameter("did");
        Integer didInt = Integer.parseInt(did);
        JsonResult jr = new JsonResult();

        DeviceService deviceService = new DeviceService();
        try {
            //问题：如何获取当前登录用户的id
            deviceService.update(fileDB,dname,setup_time,lastrepeat_time,fix_time,istrouble,didInt);
            // 一般情况，一个servlet跳转到另一个servlet用的是重定向
            jr.setCode(0);
            jr.setMsg("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("操作发生错误");
        }
        WriteJson.write(resp,jr);

    }
}
